package com.io.service.impl;

import com.io.dto.ProductReq;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
@Service
@RocketMQTransactionListener(txProducerGroup = "my-producer-group")
public class OrderServiceListener implements RocketMQLocalTransactionListener {
    @Autowired
    private OrderServiceImpl orderService;

    // 调用本地事务
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        String txId = (String) message.getHeaders().get("txId");
        ProductReq productReq = (ProductReq) o;
        orderService.transactionLocal(txId, productReq);
        return RocketMQLocalTransactionState.COMMIT;
    }

    // 回查事务
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        String txId = (String) message.getHeaders().get("txId");
        // 进行表查询
        return RocketMQLocalTransactionState.COMMIT;
    }
}
